// pages/my/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    hasUserInfo: false,
    lastTime: 0,
    pOpenid: '',

    childUser: [],
    wallet: {availableTxb: 0},
    isAuthentication: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = app.globalData.userInfo
    this.setData({userInfo:userInfo,pOpenid: app.globalData.pOpenid})
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const userInfo = app.globalData.userInfo
    if(userInfo != null && userInfo.openid !=null){
        this.getSimpleInfo()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  login: function(){
    var that = this
    app.wxPublicLogin(function(res){
        console.log("登录回调")
        that.onLoad()
        that.getSimpleInfo()
    })
    return false;
    // wx.getUserProfile({
    //   lang: 'zh_CN',
    //   desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    //   success: (res) => {
    //     console.log(res)
    //     var user = res.userInfo
    //     user['pOpenid']  = app.globalData.pOpenid
    //     this.setData({
    //       userInfo: user,
    //       hasUserInfo: true
    //     })
    //     app.globalData.userInfo = user
    //     this.saveUser(user)
    //   }
    // })
  },
  // saveUser: function(u){
  //   u['openid'] = app.globalData.openid
  //   wx.request({
  //     url: app.globalData.apiPath + 'wechat/saveUser',
  //     method: 'POST',
  //     data: u,
  //     success (res) {
  //       console.log(res)
  //       wx.showToast({title: '登录成功',icon: 'success',duration: 2000 })
  //     }
  //   })
  // },
  jumpPage: function(param){
    if(this.data.userInfo == null || this.data.userInfo == ''){
      wx.showToast({title: '请先登录',icon: 'none',duration: 2000 })
      return false;
    }
    wx.navigateTo({ url: param.currentTarget.dataset.path})
  },
  doubleClick: function(e){
    var that = this
    var thisTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    if (lastTime != 0) {
      if (thisTime - this.data.lastTime < 500){
          console.log("双击事件")
          wx.showModal({
            title: '提示',
            content: '是否重新获取微信头像、昵称等信息？',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.login()
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
      }
    }
    // 赋值
    this.setData({
      lastTime: thisTime
    })
  },
  unFinished: function(){
    wx.showToast({title: '正在开发中...',icon: 'none',duration: 3000 })
  },

  getSimpleInfo(){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'wechat/getSimpleInfo',
      method: 'POST',
      data: {openid:that.data.userInfo.openid},
      success (res) {
        console.log(123123,res)
        var wallet = res.data.data.wallet,availableTxb = 0
        if(wallet !=null && wallet.availableTxb > 0){
          availableTxb = (wallet.availableTxb/100).toFixed(2)
          wallet.availableTxb = availableTxb
        }
        that.setData({childUser: res.data.data.childUser,wallet: wallet,isAuthentication: res.data.data.isAuthentication})
        app.globalData.userInfo.isAuthentication = res.data.data.isAuthentication
      }
    })
  },


})